package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.system.domain.Building;
import com.ruoyi.system.domain.vo.ParkingSpaceVo;
import com.ruoyi.system.service.IBuildingService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.ParkingSpace;
import com.ruoyi.system.service.IParkingSpaceService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 小区车位信息Controller
 * 
 * @author 祖新宇
 * @date 2024-12-13
 */
@Controller
@RequestMapping("/system/space")
public class ParkingSpaceController extends BaseController
{
    private String prefix = "system/space";

    @Autowired
    private IParkingSpaceService parkingSpaceService;
    @Resource
    private IBuildingService buildingService;

    @RequiresPermissions("system:space:view")
    @GetMapping()
    public String space()
    {
        return prefix + "/space";
    }

    /**
     * 查询小区车位信息列表
     */
    @RequiresPermissions("system:space:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ParkingSpace parkingSpace)
    {
        startPage();
        parkingSpace.setIsDelete(0);
        List<ParkingSpace> list = parkingSpaceService.selectParkingSpaceList(parkingSpace);
        List<ParkingSpaceVo> parkingSpaceVos = new ArrayList<>();
        list.forEach(parkingSpace1 -> {
            Long buildingId = parkingSpace1.getBuildingId();
            Building building = buildingService.selectBuildingById(buildingId);
            ParkingSpaceVo parkingSpaceVo = new ParkingSpaceVo();
            BeanUtils.copyProperties(parkingSpace1,parkingSpaceVo);
            parkingSpaceVo.setBuildingName(Optional.ofNullable(building).map(Building::getBuildingName).orElse("--"));
            parkingSpaceVos.add(parkingSpaceVo);
        });
        return getDataTable(parkingSpaceVos);
    }

    /**
     * 导出小区车位信息列表
     */
    @RequiresPermissions("system:space:export")
    @Log(title = "小区车位信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ParkingSpace parkingSpace)
    {
        List<ParkingSpace> list = parkingSpaceService.selectParkingSpaceList(parkingSpace);
        ExcelUtil<ParkingSpace> util = new ExcelUtil<ParkingSpace>(ParkingSpace.class);
        return util.exportExcel(list, "小区车位信息数据");
    }

    /**
     * 新增小区车位信息
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        Building building = new Building();
        building.setIsDelete(0);
        List<Building> buildings = buildingService.selectBuildingList(building);
        mmap.put("buildings",buildings);
        return prefix + "/add";
    }

    /**
     * 新增保存小区车位信息
     */
    @RequiresPermissions("system:space:add")
    @Log(title = "小区车位信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ParkingSpace parkingSpace)
    {
        parkingSpace.setCreateTime(new Date());
        parkingSpace.setUpdateTime(new Date());
        Long userId = ShiroUtils.getUserId();
        parkingSpace.setCreateBy(String.valueOf(userId));
        parkingSpace.setUpdateBy(String.valueOf(userId));
        return toAjax(parkingSpaceService.insertParkingSpace(parkingSpace));
    }

    /**
     * 修改小区车位信息
     */
    @RequiresPermissions("system:space:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        ParkingSpace parkingSpace = parkingSpaceService.selectParkingSpaceById(id);
        mmap.put("parkingSpace", parkingSpace);
        Building building = new Building();
        building.setIsDelete(0);
        List<Building> buildings = buildingService.selectBuildingList(building);
        mmap.put("buildings",buildings);
        return prefix + "/edit";
    }

    /**
     * 修改保存小区车位信息
     */
    @RequiresPermissions("system:space:edit")
    @Log(title = "小区车位信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ParkingSpace parkingSpace)
    {
        parkingSpace.setUpdateTime(new Date());
        Long userId = ShiroUtils.getUserId();;
        parkingSpace.setUpdateBy(String.valueOf(userId));
        return toAjax(parkingSpaceService.updateParkingSpace(parkingSpace));
    }

    /**
     * 删除小区车位信息
     */
    @RequiresPermissions("system:space:remove")
    @Log(title = "小区车位信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(parkingSpaceService.softDeleteParkingSpaceByIds(ids));
    }
}
